package com.wyy.travels.controller;/*
 **
 * @author yangyang
 *@date 2022/07/21 22:50
 **/

import com.wyy.travels.entity.Province;
import com.wyy.travels.entity.Result;
import com.wyy.travels.service.ProvinceDaoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RequestMapping("province")
@RestController
@Slf4j
public class ProvinceController {
    @Autowired
    private ProvinceDaoService  provinceDaoService;
    @GetMapping("findByPage")
    public Map<String,Object> findByPage(Integer page,Integer rows){
        System.out.println(page+"========="+rows);
        page = page == null?1:page;
        rows = rows == null?4:rows;
        HashMap<String,Object> map=new HashMap<>();
        List<Province> provinces = provinceDaoService.findByPage(page, rows);
        Integer toals = provinceDaoService.findToals();
        Integer totalpage= toals%rows==0?toals/rows:toals/rows+1;
        map.put("provinces", provinces);
        map.put("toals", toals);
        map.put("totalpage", totalpage);
        map.put("page", page);
        return  map;
    }

     @PostMapping("save")
    public Result save(@RequestBody  Province province){
        Result result=new Result();
        try {
            provinceDaoService.save(province);
            result.setMsg("保存成功"+province);
        }catch (Exception e){
            e.getStackTrace();
            return  result.setState(false).setMsg(e.getMessage());
        }
        return result;
    }

    @GetMapping("delete")
    public Result delete(String id){
        Result result=new Result();
        try {
            provinceDaoService.delete(id);
            result.setMsg("删除成功"+id);
        }catch (Exception e){
            e.getStackTrace();
            return  result.setState(false).setMsg(e.getMessage());
        }
        return result;
    }
    @GetMapping("findOne")
    public  Province findOne(String id){
        Province one = provinceDaoService.findOne(id);
        return one;
    }

    @PostMapping("update")
    public Result update(@RequestBody Province province){
        Result result=new Result();
        try {
            provinceDaoService.update(province);
            result.setMsg("修改成功");
        }catch (Exception e){
            e.printStackTrace();
            result.setState(false).setMsg(e.getMessage());
        }
        return  result;
    }


}
